/**
 * the fuzzer class is responsible for fuzzing the poor requests handed to it
 * 
 */
function FuzzResponse(attackString, status, response){
    this.attackString = attackString;
    this.status = status;
    this.response = response;
    this.isXss = false;
    this.isSqli = false;
} 
 
function Fuzzer()
{
 
    this.init = 1;
    this.isFuzzing == false;
    
    this.fuzz = function(origRequest, attackType)
    {
    try{
        for( i in attackType)
        {
            var attackParts = origRequest.Url.split("=");         
            attackString = attackParts.join("=" + sp.settings.sqli[i]);
            var fuzzResult = this.sendAttack(origRequest.RequestMethod, attackString); 
            logHandler ("Fuzzing: attackString=" + attackString + " fuzz response:" + fuzzResult.status);
            
            if (sp.settings.utf8encode == true)
            {
                attackString = attackParts.join("=" + sp.fuzzer.utf8Encode(sp.settings.sqli[i]));
                var fuzzResult = this.sendAttack(origRequest.RequestMethod, attackString); 
                logHandler ("Fuzzing: attackString=" + attackString + " fuzz response:" + fuzzResult.status);
            }
            
        }    
       }catch(e){errorHandler(e);}
    }
    
    this.sendAttack = function (method, attackString)
    {
        sp.fuzzer.isFuzzing = true;      
        var newreq = new XMLHttpRequest();
        newreq.open(method, attackString, false);/* 3rd argument, true, marks this as async */
        newreq.channel.loadFlags |= Components.interfaces.nsIRequest.LOAD_BYPASS_CACHE;
        newreq.send(null);
        sp.fuzzer.isFuzzing = false; 
        return (new FuzzResponse(attackString, newreq.status, newreq.responseText)); 
    }
    
    
    this.utf8Encode =function (string) 
    {
        string = string.replace(/\r\n/g,"\n");
        var utf8text = "";

        for (var n = 0; n < string.length; n++) 
        {
            var c = string.charCodeAt(n);

            if (c < 128) {
                utf8text += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utf8text += String.fromCharCode((c >> 6) | 192);
                utf8text += String.fromCharCode((c & 63) | 128);
            }
            else {
                utf8text += String.fromCharCode((c >> 12) | 224);
                utf8text += String.fromCharCode(((c >> 6) & 63) | 128);
                utf8text += String.fromCharCode((c & 63) | 128);
            }
        alert ("UTF8" + utf8text);
        }

        return utf8text;
    }

    
    this.utf8decode = function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    } 
     
}

